package com.own.business.exam.controller;


import com.own.business.exam.business.service.ExamService;
import com.own.business.exam.entity.dto.ExamDto;
import com.own.business.exam.entity.query.ExamQuery;
import com.own.business.exam.entity.vo.ExamVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.PageModel;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.store.core.confirm.aop.ConfirmAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * ExamController
 *
 * @author chenxueli
 * @date 2021-06-18 10:21:00
 */
@Slf4j
@Tag(name = "考试" + ConstantBeanSuffix.CONTROLLER)
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + "/exam")
public class ExamController extends BaseController {

    @Resource
    private ExamService examService;

    @Operation(summary = "查询全部信息")
    @GetMapping("/all")
    public ResultModel<List<ExamVo>> all(
            @ModelAttribute ExamQuery query
    ) {
        var pageModel = examService.all(query);
        return R.success(pageModel);
    }

    @Operation(summary = "分页查询信息")
    @GetMapping("/page")
    public ResultModel<PageModel<ExamVo>> page(
            @ModelAttribute ExamQuery query
    ) {
        var pageModel = examService.page(query);
        return R.success(pageModel);
    }

    @Operation(summary = "添加考试")
    @PostMapping("/")
    public ResultModel<ExamVo> add(
            @Validated @ModelAttribute ExamDto dto
    ) {
        dto.check();
        var updateModel = examService.add(dto.toBo());
        return R.success(updateModel);
    }

    @Operation(summary = "修改考试")
    @PutMapping("/{examId}")
    public ResultModel<ExamVo> update(
            @Parameter(description = "考试id", required = true) @PathVariable Long examId,
            @Validated @ModelAttribute ExamDto dto
    ) {
        dto.check();
        var updateModel = examService.update(examId, dto.toBo());
        return R.success(updateModel);
    }

    @ConfirmAop(message = "确认删除该考试？（注意：删除之后无法找回数据）")
    @Operation(summary = "删除考试")
    @DeleteMapping("/{examId}")
    public ResultModel<Boolean> delete(
            @Parameter(description = "考试id", required = true) @PathVariable Long examId
    ) {
        var updateModel = examService.delete(examId);
        return R.success(updateModel);
    }

    @Operation(summary = "根据id获取信息")
    @GetMapping("/{examId}")
    public ResultModel<ExamVo> getById(
            @Parameter(description = "考试id", required = true) @PathVariable Long examId
    ) {
        var vo = examService.getById(examId);
        return R.success(vo);
    }

}
